<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\utils\XHtml;

/* @var $this yii\web\View */
/* @var $model app\models\AdminGroup */

$this->title = $model->isNewRecord ? '新建权限' : '更新权限';
$this->params['breadcrumbs'][] = ['label' => '权限管理', 'url' => ['group-index']];
$this->params['breadcrumbs'][] = $this->title;
?>
    <div class="page-create">
        <div class="box box-primary">
            <div class="box-body">

                <?= $this->render('/site/error-summary', ['model' => $model]) ?>

                <?php $form = ActiveForm::begin([
                    'options' => ['class' => 'form-horizontal'],
                    'fieldConfig' => [
                        'template' => "{label}\n<div class=\"col-sm-5\">{input}</div>\n<div class=\"col-sm-5\">{hint}\n{error}</div>",
                        'labelOptions' => ['class ' => 'col-sm-2 control-label'],
                    ]
                ]); ?>
                <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'status')->dropDownList(['1' => '开启', '0' => '禁用'])->label('状态') ?>

                <div class="form-group">
                    <label class="col-sm-2 control-label">权限</label>

                    <div class="col-sm-10">
                        <div class="well">
                            <dl class="mrole">
                                <?php
                                $i = 0;
                                foreach (\backend\models\Menu::getList() as $val) { ?>
                                    <dt>
                                        <?= Html::checkbox('acl[]', false, ['id' => "acl_" . $i, 'value' => $val['url'][0]]) ?>
                                        <?= Html::label($val['label'], "acl_" . $i) ?>
                                    </dt>
                                    <dd>
                                        <?php
                                        $j = 0;
                                        foreach ((array)$val['items'] as $v) {
                                            echo Html::checkbox('acl[]', false, ['id' => "acl_" . $i . "_" . $j, 'value' => $v['url'][0]]);
                                            echo Html::label($v['label'], "acl_" . $i . "_" . $j);
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                            $j++;
                                        } ?>
                                    </dd>
                                    <?php $i++;
                                } ?>
                            </dl>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label"></label>
                    <?= XHtml::submitButton($model->isNewRecord ? '创建' : '更新') ?>&nbsp;&nbsp;
                    <?= XHtml::backButton() ?>&nbsp;&nbsp;
                    <?= XHtml::resetButton() ?>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>
<?php
$script = <<<JS
    $("input[id^='acl_']").change(function () {
        var chk = $(this).is(":checked");
        var id = $(this).attr("id");
        $("input[id^='" + id + "_']").prop("checked", chk);
        if (chk) {
            var strs = id.split("_");
            var id2 = strs[0] + "_" + strs[1];
            $("input[id='" + id2 + "']").prop("checked", chk);
            if (strs.length == 4) {
                var id3 = strs[0] + "_" + strs[1] + "_" + strs[2];
                $("input[id='" + id3 + "']").prop("checked", chk);
            }
        }
    });
    var acls = "$model->acl";
    var acl = acls.split(',');
    $("input[id^='acl_']").each(function () {
        var v = $(this).val();
        if (acl.indexOf(v) >= 0) {
            $(this).prop("checked", true);
        }
    });
JS;
$this->registerJs($script);
?>